package com.example.queryexpressdelivery_01.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.queryexpressdelivery_01.entity.AdminData;
import com.example.queryexpressdelivery_01.common.DeliveryDAL;
import com.example.queryexpressdelivery_01.R;
import com.example.queryexpressdelivery_01.entity.UserData;

import java.util.List;

public class LoginActivity extends AppCompatActivity {

    private EditText edtLoginName;
    private EditText edtLoginPassword;
    private Button btnLoginLogin;
    private Button btnLoginRegister;

    private DeliveryDAL deliveryDAL = new DeliveryDAL(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();
        btnLoginLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = edtLoginName.getText().toString().trim();
                String password = edtLoginPassword.getText().toString().trim();
                List<UserData> userDataList = deliveryDAL.queryUserAll();
                List<AdminData> adminDataList = deliveryDAL.queryAdminAll();
                boolean is_success = false;
                for (UserData user:userDataList){       // 判断用户账号密码是否正确
                    if (account.equals(user.getAccount()) && password.equals(user.getPassword())){
                        is_success = true;
                        Toast.makeText(LoginActivity.this,"登录成功！",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(LoginActivity.this, UserActivity.class);
                        intent.putExtra("id",user.getId());
                        startActivity(intent);
                        finish();
                    }
                }
                if (!is_success){
                    for (AdminData admin:adminDataList){        // 判断管理员的账号密码是否正确
                        if (account.equals(admin.getAccount()) && password.equals(admin.getPassword())){
                            is_success=true;
                            break;
                        }
                    }
                    if (is_success){
                        Toast.makeText(LoginActivity.this,"登录成功！",Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(LoginActivity.this, AdminActivity.class));
                        finish();
                    }
                }
                if (!is_success)Toast.makeText(LoginActivity.this,"账号或密码错误！",Toast.LENGTH_SHORT).show();
            }
        });

        btnLoginRegister.setOnClickListener(new View.OnClickListener() {        // 跳转到注册页面
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                finish();
            }
        });


    }

    private void initView() {
        edtLoginName = (EditText) findViewById(R.id.edtLoginName);
        edtLoginPassword = (EditText) findViewById(R.id.edtLoginPassword);
        btnLoginLogin = (Button) findViewById(R.id.btnLoginLogin);
        btnLoginRegister = (Button) findViewById(R.id.btnLoginRegister);
    }
}